package com.tiantian.quanminxiu.adapter;

import android.app.Activity;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.tiantian.quanminxiu.R;
import com.tiantian.quanminxiu.utils.APPUtils;

import org.json.JSONArray;
import org.json.JSONObject;

/**
 * Created by admin on 2016/1/14.
 */
public class FanRollGuardAdapter extends RecyclerView.Adapter<FanRollGuardAdapter.MyViewHolder>
{

    private Activity activity;
    private JSONArray list;
    private int Room_id;
    private String NickName;
    public FanRollGuardAdapter(Activity activity, JSONArray list,int room_id,String NickName){
        this.activity = activity;
        this.list = list;
        this.Room_id = room_id;
        this.NickName = NickName;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        MyViewHolder holder = new MyViewHolder(LayoutInflater.from(
                activity).inflate(R.layout.activity_fanroll_guarditem, parent,
                false));
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position)
    {
        JSONObject user = list.optJSONObject(position);
        if(position==0){
            holder.txtUserName.setTextColor(Color.parseColor("#424242"));
            holder.txtUserName.setText("开通守护");
            APPUtils.displayImage("drawable://" + R.drawable.icon_fanroll_open, holder.imgUserAvatar, false, false);
            holder.vContent.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    APPUtils.Dialog_OpenGuard_To(activity,""+Room_id,NickName);
                }
            });
        }else {
            holder.txtUserName.setText(user.optString("NickName"));
            holder.txtUserName.setTextColor(Color.parseColor("#424242"));
            holder.imgUserAvatar.post(new Runnable() {
                @Override
                public void run() {
                    APPUtils.displayImage(APPUtils.createUserAvatar(user.optInt("User_ID")), holder.imgUserAvatar, activity.getBaseContext(), holder.imgUserAvatar.getHeight() / 2);
                }
            });
            holder.vContent.setTag(user);
            holder.vContent.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    APPUtils.Dialog_UserInfoCard_To(activity,user.optInt("User_ID"),user.optString("NickName"),-1,user.optInt("Level"),-1,false,false);
                }
            });
        }
    }

    @Override
    public int getItemCount()
    {
        return list.length();
    }

    class MyViewHolder extends RecyclerView.ViewHolder
    {
        TextView txtUserName;
        ImageView imgUserAvatar;
        View vContent;

        public MyViewHolder(View view)
        {
            super(view);
            vContent = view.findViewById(R.id.vContent);
            imgUserAvatar = (ImageView) view.findViewById(R.id.imgUserAvatar);
            txtUserName = (TextView) view.findViewById(R.id.txtUserName);
        }
    }

}
